const http = require('http');
const fs = require('fs');
const path = require('path');
const serve = http.createServer();
const config = require('./config');
const Parser = require('../lib/compiler-dom')
const pth = path.resolve(path.join(process.cwd(), config.sourcePth));
const containPth = [];
const domContain = [];
function rootDir(pth, contain) {
    const files = fs.readdirSync(pth, { encoding: 'utf-8', withFileTypes: true });
    files.forEach(item => {
        if (item.isDirectory()) {
            rootDir(path.join(pth, '/', item.name), contain);
        } else if (item.isFile()) {
            contain.push(path.join(pth, '/', item.name));
        }
    })
}
rootDir(pth, containPth);

/**
 * 处理文件
 */
function handleFile() {
    containPth.forEach(file => {
        const content = readFile(file);
        domContain.push(content);
    })
}
handleFile();

/**
 * 读取dom 
 * @param {*} fileName 
 * @returns 
 */
function readFile(fileName) {
    const data = fs.readFileSync(fileName, { encoding: 'utf-8' });
    const parser = new Parser(data);
    parser.parser();
    return {
        filePath: fileName,
        tag: parser.tags
    };
}


/**
 * 服务
 */
serve.on('request', (req, res) => {
    const url = new URL(req.url);
    if (url.hostname === 'localhost') {
        res.write()
    }
})
serve.listen(3000)
